<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <treeTools :tree-node="company" :is-root="true" @addDepts="AddDepts" />
        <hr>
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <treeTools slot-scope="{ data }" :tree-node="data" @delDapartments="getDapartments" @addDepts="AddDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <addDept ref="addDept" :showdialog.sync="showDialog" :tree-node="node" @addDepts="getDapartments" />
  </div>
</template>

<script>
import addDept from '@/views/departments/components/add-dept.vue'
import treeTools from '@/views/departments/components/tree-tools.vue'
import { getDapartments } from '@/api/departments'
import { listToTreeData } from '@/utils/index'
export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      node: {},
      showDialog: false,
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      company: {}
    }
  }, created() {
    this.getDapartments()
  },
  methods: {
    // 封装的函数
    async getDapartments() {
      this.loading = true
      const res = await getDapartments()
      console.log(55, res)
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.departs = listToTreeData(res.depts, '')
      this.loading = false
    },

    // 新增弹出弹框
    AddDepts(node) {
      this.showDialog = true
      this.node = node
    },

    // 编辑时弹窗弹出
    async editDepts(node) {
      await this.$refs.addDept.getDepartmentsDetail(node.id)
      this.showDialog = true
      this.node = node
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
